package com.fofia.iotweb.web.dbdomain.services.admin;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.fofia.iotweb.Contants.DataStatusEnum;
import com.fofia.iotweb.framework.dto.ZtreeDto;
import com.fofia.iotweb.web.dto.ResultAdaptDto;
import com.fofia.iotweb.web.dbdomain.mappers.admin.SysDeptMapper;
import com.fofia.iotweb.web.dbdomain.models.admin.SysDept;
import com.fofia.iotweb.web.dto.LoginUserDto;
import com.mybatisflex.core.query.QueryWrapper;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.dynamicds.DynamicDs;

import java.util.ArrayList;
import java.util.List;

@Component
public class SysDeptService {

    @Inject("db")
    private SysDeptMapper deptMapper;


    public List<SysDept> getList(String deptName){

       QueryWrapper query=QueryWrapper.create();
        if(!StrUtil.isEmpty(deptName)){
            query.like(SysDept::getDeptName,deptName);
        }
        query.orderBy(SysDept::getOrderNum);

        List<SysDept> list= deptMapper.selectListByQuery(query);
        return list;
    }


    public SysDept getModel(int id){

       SysDept dept= deptMapper.selectOneById(id);
       return dept;
    }

    /**
     *
     * @author yushuo
     * @description 保存数据
     * @date 14:25 2021/2/26
     * @param [dept]
     * @return void
     **/
    public void save(SysDept dto, LoginUserDto loginUserDto){
        dto.setUpdateBy(loginUserDto.getLoginName());
        dto.setUpdateTime(DateUtil.date());
        dto.setAncestors("");
        if(dto.getLeaderId()==null){
           dto.setLeaderId(0L);
           dto.setLeader("");
        }
        if(dto.getId()==0){
            dto.setCreateBy(loginUserDto.getLoginName());
            dto.setCreateTime(DateUtil.date());
            dto.setDelFlag(DataStatusEnum.NOT_DELETED.getCode());
            deptMapper.insert(dto);
            return;
        }

        deptMapper.update(dto);
    }

    /**
     *
     * @author yushuo
     * @description 删除数据
     * @date 14:47 2021/2/26
     * @param [id]
     * @return void
     **/
    public ResultAdaptDto delete(int id){
        ResultAdaptDto ret=new ResultAdaptDto();
       QueryWrapper query=QueryWrapper.create()
               .eq(SysDept::getParentId,id)
                .orderBy(SysDept::getId).asc()
                .limit(0,1);;


        SysDept dept=deptMapper.selectOneByQuery(query);
        if(dept!=null){
            ret.setStatus(false);
            ret.setMessage("该部门下面有子部门");
            return ret;
        }
        deptMapper.deleteById(id);
        return ret;
    }

    public  List<ZtreeDto> getDeptZtree(){

        List<SysDept> list=getList("");
        List<ZtreeDto> data=new ArrayList<>();
        list.forEach(s->{
            ZtreeDto ztreeDto=new ZtreeDto();
            ztreeDto.setId(String.valueOf(s.getId()));
            ztreeDto.setName(s.getDeptName());
            ztreeDto.setPId(String.valueOf(s.getParentId()));
            data.add(ztreeDto);
        });

        return data;
    }
}
